package pl.enigmatic.test;

import javax.swing.BorderFactory;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public abstract class Slider extends JSlider {

	/** */
	private static final long serialVersionUID = 1L;

	private double last;

	public Slider(final String text) {
		setBorder(BorderFactory.createTitledBorder(text));
		setMajorTickSpacing(20);
		setMinorTickSpacing(1);
		setPaintTicks(true);
		setPaintLabels(true);
		addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(final ChangeEvent e) {
				refresh();
			}
		});
		last = getAlpha();
	}

	private void refresh() {
		final double alpha = getAlpha();
		refresh(alpha, alpha - last, (1 + alpha) / (1 + last));
		last = alpha;
	}

	protected abstract void refresh(double alpha, double delta, double factor);

	public double getAlpha() {
		return getValue() / (double) (getMaximum() - getMinimum());
	}

	public Slider setAlpha(final double alpha) {
		setValue((int) (alpha * (getMaximum() - getMinimum())));
		return this;
	}
}
